package com.view
{
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.events.MyEvent;
	import com.model.ModelLocator;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.DataGrid;
	import mx.events.ListEvent;
	
	import utils.FileUtil;
	
	public class MusicListClass extends VBox
	{
		public var songListGrid:DataGrid;
		public var loadBtn:Button;
		
		private var _fileReference:FileReference = new FileReference();
		
		private var songList:Array;
		
		private var _model:ModelLocator;
		
		public function MusicListClass()
		{
			_model = ModelLocator.getInstance();
			songList = _model.songList;
		}
		
		public function init():void
		{
			songListGrid.dataProvider = new ArrayCollection(songList);
			initEventListener();
		}
		
		private function initEventListener():void
		{
			songListGrid.addEventListener(ListEvent.ITEM_CLICK, onItemClick);
			loadBtn.addEventListener(MouseEvent.CLICK, onLoadHandler);
			
		}
		
		protected function onLoadHandler(event:MouseEvent):void
		{
//			FileUtil.onLoadFile(respondToLoadFile);
			var fileFilter:FileFilter = new FileFilter("*.mp3", "*.mp3");
			_fileReference.browse([fileFilter]);
			_fileReference.addEventListener(Event.SELECT, selectHandler);
		}
		
		private function respondToLoadFile(data:*):void
		{
			trace("......", data);
		}
		
		protected function selectHandler(event:Event):void
		{
			_fileReference.removeEventListener(Event.SELECT, selectHandler);
			_fileReference.addEventListener(Event.COMPLETE, loadCompleteHandler);
			_fileReference.load();
		}
		
		private function loadCompleteHandler(event:Event):void
		{
			_fileReference.removeEventListener(Event.COMPLETE, loadCompleteHandler);
			//   播放mp3 已经 歌词
			//   _fileReference.data
			Alert.show(_fileReference.name + " 已经加载完成");
		}
		
		private function onItemClick(event:ListEvent):void
		{
			CairngormEventDispatcher.getInstance().dispatchEvent(new MyEvent(MyEvent.PLAY, event.target.selectedItem));			
		}
	}
}